<script setup lang="ts">
import { computed, PropType, ref } from 'vue'
type ListType = Record<'url' | 'title' | 'path', string>

const $props = defineProps({
  list: {
    type: Array as PropType<ListType[]>,
    default: () => [],
  },
})
const renderList = computed(() => {
  if (!$props.list.length) return []
  if ($props.list.length > 10) {
    return twoArrays($props.list)
  } else {
    return [$props.list]
  }
})
const currentIndex = ref(0)

const swiperChangeHandle = (e: { detail: { current: number } }) => {
  currentIndex.value = e.detail.current
}
/**
 * 一维转二维数组
 */
function twoArrays(arr: ListType[]) {
  const newArr: ListType[][] = []
  for (let i = 0; i < arr.length; i += 10) {
    newArr.push(arr.slice(i, i + 10))
  }
  return newArr
}
</script>

<template>
  <view style="margin: 20rpx 0">
    <swiper style="height: 320rpx" class="swiper" :current="currentIndex" @change="swiperChangeHandle">
      <swiper-item v-for="(item, index) in renderList" :key="index" class="swiper__item">
        <view v-for="ite in item" :key="ite.url" class="swiper__content">
          <view class="swiper__content-item">
            <image class="swiper__content-item--img"></image>
            <view class="swiper__content-item--title">{{ ite.title }}</view>
          </view>
        </view>
      </swiper-item>
    </swiper>
    <view class="indicator">
      <view class="indicator__bg" :style="{ width: `${renderList.length * 42}rpx` }">
        <view class="indicator__bg--red" :style="{ left: `${currentIndex * 42}rpx` }"></view>
      </view>
    </view>
  </view>
</template>

<style lang="scss" scoped>
@include b(swiper) {
  @include e(item) {
    box-sizing: border-box;
    display: grid;
    grid-template-columns: repeat(5, 142rpx);
    grid-column-gap: 0rpx;
  }
  @include e(content-item) {
    @include flex();
    flex-direction: column;
    @include m(img) {
      width: 118rpx;
      height: 118rpx;
      border-radius: 50%;
      background: red;
    }
    @include m(title) {
      font-size: 24rpx;
      color: #6a6a6a;
      text-align: center;
    }
  }
}
@include b(indicator) {
  @include flex();
  @include e(bg) {
    height: 10rpx;
    border-radius: 40rpx;
    background: #d6d8de;
    position: relative;
    @include m(red) {
      width: 42rpx;
      height: 10rpx;
      border-radius: 40rpx;
      position: absolute;
      background: #f12f22;
      transition: all 0.4s;
    }
  }
}
</style>
